<template>
  <view class="container">
    <js-error mode="bottom" ref="jsError"></js-error>
    <!-- <view class="search">
			<u-search @click="show = true" :show-action="false" placeholder="搜索" :disabled="true"></u-search>
		</view> -->
    <!-- <scroll-view  style="height: 100vh;" scroll-y="true"  :refresher-enabled="true"
			:refresher-triggered="triggered" @refresherrefresh="refresherrefresh" > -->
    <view
      v-if="list.length"
      style="
        display: flex;
        margin: 10px;
        background-color: #fff;
        padding: 10px;
        border-radius: 10px;
      "
      v-for="(item, index) in list"
      :key="index"
      @click="goRouter(item)"
    >
      <view style="position: relative; display: flex; align-items: center">
        <u-badge
          v-if="item.count != '0'"
          type="error"
          :is-dot="true"
          :offset="[6, 6]"
        ></u-badge>
        <image
          style="width: 120rpx; height: 120rpx"
          :src="'/static/image/notice/' + item.img + '.png'"
          mode="aspectFit"
        />
      </view>
      <view
        style="
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          flex: 1;
          padding: 3px 10px;
        "
      >
        <view
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <view style="font-weight: bold" class="text-lg">
            {{ item.dictName }}
          </view>
          <view style="color: #999999">
            <u-badge
              type="error"
              :absolute="false"
              :count="item.count"
              fontSize="32"
            ></u-badge>
            <!-- count="1" -->
          </view>
        </view>
        <view
          style="
            display: flex;
            justify-content: space-between;
            align-items: center;
          "
        >
          <view
            style="
              flex: 1;
              color: #999999;
              font-size: 14px;
              display: -webkit-box;
              -webkit-box-orient: vertical;
              -webkit-line-clamp: 1;
              overflow: hidden;
              text-overflow: ellipsis;
            "
          >
            {{ getJsonObjectFromMsg(item.msg)['msgTitle'] ? getJsonObjectFromMsg(item.msg)['msgTitle'] : '暂无新消息' }}
          </view>
          <view>
            {{ getDate(getJsonObjectFromMsg(item.msg)['sendDate']) }}
            
          </view>
        </view>
      </view>
    </view>
    <view style="height: 1px"></view>
    <view style="height: 108rpx"></view>
    <custom-tab-bar ref="customtab1" :current-index="currentIndex" @changeTab="handleTabChange" v-if="customTabBar" /> 
  </view>
</template>

<script>
import customTabBar from '@/pages/zfgs/index/componetns/customTabBar.vue';
export default {
  components: {
    customTabBar
  },
  data() {
    return {
      triggered: false,
      show: false,
      smshow: false,
      focus: true,
      barCode: "",
      list: [
      ],
      query: {
        pageNo: 1,
        pageSize: 20,
        // orderBy: "a.create_date desc",
      },
      currentIndex:1,
      loadStatus: "loadmore",
      customTabBar:true,
    };
  },

  onLoad() {
    console.log("onLoad");
    this.query.pageNo = 1;
    this.loadData();
    this.customTabBar = false ;
    setTimeout(()=>{
        this.customTabBar = true ;
        // this.$refs.customtab1.getcountData()
        // this.$refs.customtab1.getMsgCount()
        if (this.$refs.customtab1) {
            this.$refs.customtab1.getcountData();
            this.$refs.customtab1.getMsgCount();
        }
    },100)
  },
  onShow() {
    this.query.pageNo = 1;
    this.customTabBar = false ;
    setTimeout(()=>{
        this.customTabBar = true ;
        // this.$refs.customtab1.getcountData()
        // this.$refs.customtab1.getMsgCount()
        if (this.$refs.customtab1) {
            this.$refs.customtab1.getcountData();
            this.$refs.customtab1.getMsgCount();
        }
    },100)

    
    // if(this.customTabBar){
    //   console.log("onShow");
    //   this.customTabBar = false;
    //   setTimeout(()=>{
    //     console.log("onLoad",this.customTabBar);
    //     this.customTabBar = true;
    //   },100)
    // }else{
    //   this.customTabBar = true;
    // }
    this.loadData();
    console.log("onShow",this.$refs)
  },
  methods: {
    handleTabChange(pagePath) {
      // 切换页面逻辑，可使用uni.navigateTo等API
      uni.reLaunch({ url: pagePath });
    },
    reset() {
      this.query = {
        pageNo: 1,
        pageSize: 20,
      };
      this.loadData();
    },
    loadMore() {
      this.loadStatus = "loading";
      setTimeout(() => {
        this.query.pageNo += 1;
        this.loadData("add");
      }, 100);
    },
    async refresherrefresh() {
      this.triggered = true;
      this.query.pageNo = 1;
      await this.loadData();
      setTimeout(() => {
        this.triggered = false;
      }, 500);
      // this.triggered = false;
    },
    loadData(type) {
      this.$u.api.zfgs.findTypeList().then((res) => {
        this.show = false;
        if (res.data.length < 20 || res.data.length == 0) {
          this.loadStatus = "nomore";
        }
        var data = res.data;
        if (type == "add") {
          for (var i = 0; i < data.length; i++) {
            this.list.push(data[i]);
          }
        } else {
          this.list = data;
        }
      });
    },
    submit() {
      setTimeout(() => {
        this.query.pageNo = 1;
        this.loadData();
      }, 100);
    },
    goRouter(item) {
      console.log(item,'item')
      const bizType = this.getJsonObjectFromMsg(item.msg)['bizType']
      const dictName = item.dictName
      const dictValue = item.dictValue
      const img = item.img
      const params = {
        // bizType: bizType,
        bizType: dictValue,
        dictName: dictName,
        img: img,
      };
      // 跳转到列表页
      if(!!bizType){
      uni.navigateTo({
        url: "/pages/zfgs/index/notice/list?params=" + JSON.stringify(params),
      });
	  }else{
      uni.navigateTo({
        url: "/pages/zfgs/index/notice/list?params=" + JSON.stringify(params)+"&current= 1",
      });
    }
    },
	getJsonObjectFromMsg(msg) {
         try {
			// console.log(JSON.stringify(msg));
           return msg ? JSON.parse(JSON.stringify(msg)) :'';
         } catch (error) {
           console.error('Failed to parse msg:', error);
           return {};
         }
    },
	// 年月日只显示月日
	getDate(date) {
		if (date) {
			var date = new Date(date);
			var year = date.getFullYear();
			var month = date.getMonth() + 1;
			var day = date.getDate();
			var hour = date.getHours();
			var minute = date.getMinutes();
			var second = date.getSeconds();
      // 日后month小于10，前面加个0
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
			return month + "-" + day;
		} else {
			return "";
		}
	},
  },
};
</script>

<style lang="scss">
</style>